package com.example.spring.Util;

import com.example.spring.entity.UserBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class DBUtil {
    // Map 模拟数据库
    private final Map<String, UserBean> users = new HashMap<>();

    // 私有化构造方法，防止外部创建对象
    private DBUtil() {
        users.put("Jack", new UserBean("Jack", "jack@it315.org", "123456"));
        users.put("Rose", new UserBean("Rose", "rose@it315.org", "abcdefg"));
    }

    public UserBean getUserByName(String userName) {
        return users.get(userName);
    }

    public boolean insertUser(UserBean user) {
        // 判断user和user.name是否为null，如不为null，判断用户是否已经存在
        if (user == null || user.getName() == null || users.get(user.getName()) != null) {
            return false;
        }
        // 插入用户
        users.put(user.getName(), user);
        return true;
    }
}
